package test.匹配pmid图片相关;

import cn.hutool.core.collection.ConcurrentHashSet;
import cn.hutool.core.io.FileUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.ds.druid.DruidDSFactory;
import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import utils.SmallTool;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.stream.IntStream;

/**
 * @Author: gcr
 * @Date: 2022/4/18 14:30
 */
public class 获取最新数据4 {
    public static String BossTable = "pmiddoidown_gcr_local";
    public static String todownTable = "to_down";

    @SneakyThrows
    public static void main(String[] args) {
        // vm options -Xmx18660m -Xms12560m

        SmallTool.printMemory();

        String serverTable = "pmid_figure";
        DSFactory.setCurrentDSFactory(new DruidDSFactory());
        DataSource ds = DSFactory.get("server_local");
        Db use = Db.use(ds);

        long s1 = System.currentTimeMillis();
        // 并行计算数量
        Set<Integer> localPmids = new ConcurrentHashSet<>();
        Set<Integer> serverPmids = new ConcurrentHashSet<>();

        CompletableFuture completableFutureLocal = CompletableFuture.runAsync(() -> {
            String allDate = "select max(pmid) as pmid  from " + BossTable;
            Entity query = null;
            try {
                query = use.query(allDate).get(0);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            SmallTool.printMess("最大的id 是" + query.getStr("pmid"));
            Integer count = query.getInt("pmid");
            int breakSize = 100000;
            int pageSize = count / breakSize + 1;
            SmallTool.printMess("全部页码+++++++++" + pageSize);

            SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");

            IntStream.rangeClosed(1, pageSize).parallel().forEach(i -> {
                int start = breakSize * (i - 1) + 1;
                int end = i * breakSize;
                String paramSql = "select * from " + BossTable + " where  pmid between  " + start + " and " + end;
                List<Entity> querys = null;
                try {
                    querys = use.query(paramSql);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                querys.parallelStream().forEach(entity -> {
                    Integer pmid = entity.getInt("pmid");
                    localPmids.add(pmid);

                });
            });

        });

        CompletableFuture completableFutureServer = CompletableFuture.runAsync(() -> {
            String allDate = "select max(pmid) as pmid  from " + serverTable;
            Entity query = null;
            try {
                query = use.query(allDate).get(0);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            SmallTool.printMess("最大的id 是" + query.getStr("pmid"));
            Integer count = query.getInt("pmid");
            int breakSize = 100000;
            int pageSize = count / breakSize + 1;
            SmallTool.printMess("全部页码+++++++++" + pageSize);
            SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");

            IntStream.rangeClosed(1, pageSize).parallel().forEach(i -> {
                int start = breakSize * (i - 1) + 1;
                int end = i * breakSize;

                String paramSql = "select pmid from " + serverTable + " where  pmid between  " + start + " and " + end;
                List<Entity> querys = null;
                try {
                    querys = use.query(paramSql);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                querys.parallelStream().forEach(entity -> {
                    Integer pmid = entity.getInt("pmid");
                    serverPmids.add(pmid);
                });
            });

        });


        CompletableFuture.allOf(completableFutureServer, completableFutureLocal).get();
        SmallTool.printMessLongTimeTake("两个集合完毕++", s1);
        SmallTool.printMess("新的本地数据++" + localPmids.size());
        SmallTool.printMess("新的服务器数据++" + serverPmids.size());
        FileUtil.writeString(JSON.toJSONString(localPmids), "/usr/local/20220418/localPmids.log", "UTF-8");
        FileUtil.writeString(JSON.toJSONString(serverPmids), "/usr/local/20220418/serverPmids.log", "UTF-8");

        localPmids.removeAll(serverPmids);
        SmallTool.printMess("删除后的++" + localPmids.size());


        localPmids.parallelStream().forEach(o -> {
            Entity entity = new Entity();
            entity.setTableName(todownTable);
            entity.set("pmid", o);
            entity.set("figure", getF(o, use));
            entity.set("status", 0);
            try {
                use.insert(entity);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        });


        SmallTool.printMess("结束了");


    }

    @SneakyThrows
    public static String getF(Integer pmid, Db use) {
        Entity entity = use.queryOne("select * from " + BossTable + " where pmid = ?", pmid);
        return entity.getStr("figure");

    }
}
